#include "ErrorCode.h"
#include "Config.h"
#include "PublicFunction.h"
#include "Define.h"

namespace gitstore {
std::shared_ptr<CErrorCodeManager> CErrorCodeManager::_instance = std::shared_ptr<CErrorCodeManager>(new CErrorCodeManager);

CErrorCodeManager::CErrorCodeManager() {
    loadErrorMessage();
}

CErrorCodeManager::~CErrorCodeManager() {

}

void CErrorCodeManager::loadErrorMessage() {
    std::string content;
    readFile(ERROR_CONFIG_PATH, content);
    std::vector<std::string> verr;
    splitString(content, verr, "\n");
    std::for_each(verr.begin(), verr.end(), [&](std::vector<std::string>::reference err){
        std::vector<std::string> cols;
        splitString(err, cols, ",");
        this->_map_err_msg[static_cast<ErrorStatus>(std::stoi(cols[1]))] = cols[2];
    });
}

std::shared_ptr<CErrorCodeManager> CErrorCodeManager::getInstance() {
    return _instance;
}

std::string CErrorCodeManager::getErrorMessage(ErrorStatus code) {
    return _map_err_msg.at(code);
}

//ErrorCode::ErrorCode()
//    :ErrorCode(Success) {

//}

//ErrorCode::ErrorCode(ErrorStatus s)
//    : status(s), msg(CGlobalConfig::getInstance()->getErrorMessage(status)) {

//}

//std::string ErrorCode::toJsonString()
//{
//    std::string res("{\"code\":");
//    res += std::to_string(status);
//    res += ",\"message\":\"";
//    res += msg;
//    res += "\"}";
//    return res;
//}

//ErrorCode::operator std::string() const
//{
//    std::string res("{\"code\":");
//    res += std::to_string(status);
//    res += ",\"message\":\"";
//    res += msg;
//    res += "\"}";
//    return res;
//}

}
